package com.yingli.component.activity.transfer;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.yingli.component.R;
import com.yingli.component.activity.transfer.entity.Book;
import com.yingli.component.activity.transfer.entity.User;
import com.yingli.component.eventbus.MessageEvent;
import com.yingli.component.service.FirstService;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class FirstActivity extends AppCompatActivity {

    private static final int RESULT = 1024;

    private static final String TAG = FirstActivity.class.getSimpleName();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        Log.i("lifecycle",TAG+"onCreate");
//        EventBus.getDefault().register(this);
        findViewById(R.id.btn_carry_data).setOnClickListener(v->{
//            Intent intent = new Intent(this,SecondActivity.class);
//            intent.putExtra("value",10);
//            User user = new User();
//            user.setName("张三");
//            user.setPosition("CEO");
//            user.setSalary(1000000);
//            //传递Serializable对象
//            intent.putExtra("user",user);
//            Book book = new Book();
//            book.setAuthor("李阳");
//            book.setName("疯狂英语");
//            book.setPrice(199.8);
//            intent.putExtra("book",book);
//            startActivityForResult(intent,RESULT);
//            startService(new Intent(this, FirstService.class));
            EventBus.getDefault().post(new MessageEvent("这里是传递的数据"));
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("lifecycle",TAG+"onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("lifecycle",TAG+"onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("lifecycle",TAG+"onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("lifecycle",TAG+"onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("lifecycle",TAG+"onDestroy");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case RESULT:
                if (data!=null) {
                    //携带的数据返回上一个界面
                    Book book = data.getParcelableExtra("b");
                    Log.i("bookResult",book.toString());
                }
                break;
        }
    }
}
